<?php
/**
 * “新浪微博”刷粉丝
 * 
 * @author linsir123
 */
class LxsWeibo
{
	/**
	 * @var 客户端
	 */
	public $client;
	
	/**
	 * @var 当前用户
	 */
	public $me;
	
	/**
	 * @var 收听队列列表
	 */
	public $friends;
	
	/**
	 * @var 粉丝队列列表
	 */
	public $followers;
	
	/**
	 * @var 无法取消列表缓存文件
	 */
	public $missFile;
	
	/**
	 * @var 无法取消列表
	 */
	public $miss;
	
	/**
	 * @var 应用索引
	 */
	public $index;
	
	/**
	 * 构造函数
	 */
	function __construct($index, $apps)
	{
		include_once( 'weibooauth.php' );
		
		/// 
		ob_start();
		session_start();
		set_time_limit(0);
		header('Content-Type: text/html; charset=utf-8');
		if (function_exists('date_default_timezone_set')) 
			date_default_timezone_set('Asia/ShangHai');
			
		///
		$h = intval(date('H'));
		if ($h > 23 || $h < 7) 
			exit();
			
		$i = ($h >= 8 && $h <= 12) ? 1 : 0;
		include(dirname(__FILE__).'/'.$apps[$i][0]);
		
		///
		$this->client = new WeiboClient(WB_AKEY , WB_SKEY , $apps[$i][1][0], $apps[$i][1][1]);
		$this->me = $this->client->verify_credentials();
		$this->index = $index;
		
		///
		$data = $this->client->friends_ids(null, 2000);
		$this->friends = $data['ids'];
		$data = $this->client->followers_ids(null, 2000);
		$this->followers = $data['ids'];
		
		///
		$this->missFile = dirname(__FILE__)."/_miss{$index}.php";
		$this->miss = file_exists($this->missFile) ? require($this->missFile) : array();
	}
	
	/**
	 * 开始
	 */
	function run()
	{
		pr("----------------------".date('Y-m-d H:i:s')."----------------------");
		pr($this->me);
		$this->unFollow();
		$this->follow();
	}
	
	/**
	 * 取消关注
	 */
	function unFollow($num = 20)
	{
		pr("----------------------unFollow----------------------");
		
		///
		$count = intval($this->me['friends_count']);
		$i = $count - 1;
		$success = 1;
		if ($count >= 1900) {
			while ($success <= $num && $i >= 0) {
				$id = "{$this->friends[$i]}";
				if ( ! isset($this->miss[$id])) {
					$result = $this->client->unfollow($id);
					$this->writeLog($id, $result, '_unFollow');
					if ( ! isset($result['error'])) {
						$success++;
					} else {
						if ($result['error'] == "40028:fuid错误") {
							$this->miss[$id] = 1;
						} else {
							$success++;
						}
					}
				}
				
				///
				$i--;
			}
		}
		
		$fp = fopen($this->missFile, "w+");
		fwrite($fp, "<?php return ".var_export($this->miss, true).";");
		fclose($fp);
	}
	
	/**
	 * 关注
	 */
	function follow($num = 10)
	{
		$uid = $this->getUser();
		pr("----------------------follow[{$uid}]----------------------");
		
		///
		$count = intval($this->me['friends_count']);
		if ($count > 2000) 
			return ;
			
		///
		$dis = 2000 - $count;
		if ($dis < $num) 
			$num = $dis;
			
		///
		$success = 1;
		$tmp = array();
		$data = $this->client->followers_ids(null, $num+10, $uid);
		if (is_array($data['ids'])) {
			foreach ($data['ids'] as $id) {
				$flag = in_array($id, $this->friends);
				$flag = $flag || in_array($id, $this->followers);
				$flag = $flag || in_array($id, $tmp);
				$flag = $flag || $success > $num;
				if ($flag) 
					continue;
					
				///
				$tmp[] = $id;
				$result = $this->client->follow($id);
				$this->writeLog($id, $result, '_follow');
				$success++;
				
				///
				if (isset($result['error']) && strpos($result['error'], '40028:hi 超人') !== false) 
					break;
			}
		}
	}
	
	/**
	 * 取得某个名人
	 */
	function getUser()
	{
		$list =	array (
			0 => '1644395354',
			1 => '1713926427',
			2 => '1644572034',
			3 => '1252373132',
			4 => '1735618041',
			5 => '1671526850',
			6 => '1657421782',
			7 => '1660209951',
			8 => '1236380405',
			9 => '1675718025',
			10 => '1642635773',
			11 => '1657430300',
			12 => '1097201945',
			13 => '1567852087',
			14 => '1615743184',
		);
		
		///
		$i = mt_rand(0, count($list) - 1);
		return $list[$i];
	}
	
	/**
	 * 日志
	 */
	function writeLog($id, $result, $name = '_follow')
	{
		static $i = 0;
		
		///
		++$i;
		if ( ! isset($result['error'])) {
			pr($i. ": " .$result['id']);
		} else {
			pr($i. ": " .$id);
			pr($result);
		}
	}
}

/**
 * 调试
 */
function pr($var)
{
	echo '<pre>';
	print_r($var);
	echo '</pre>';
	ob_flush();
    flush();
}
